<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.twoscreen.PlayerScreen"%>
<%@ page import="com.twoscreen.Event"%>
<%
	//Variables
	String videoId = request.getParameter("videoId");
	String playerScreenID;
	String thisURL = request.getRequestURL().toString();
	
	//If no video ID is passed redirect to homepage
	if (videoId == null) {
		response.sendRedirect("/");
	}

	//Check if there is a psID or policy cookie and get it
	Cookie psIDcookie = null;
	Boolean showCookieNotice = true;
	Cookie cookies[] = request.getCookies();
	if (cookies != null) {
		for (int i=0 ; i<cookies.length ; i++) {
			if (cookies[i].getName().equals("psID")) {
				psIDcookie = cookies[i];
			}
			if (cookies[i].getName().equals("cookie-dismiss")) {
				showCookieNotice = false;
			}
		}
	}
	
	//If there is a cookie use the psID if not get a new one
	if (psIDcookie != null) {
		playerScreenID = psIDcookie.getValue();
	} else {
		playerScreenID = PlayerScreen.getPlayerScreenId();
		//Set the psID in a cookie
		Cookie playerScreen = new Cookie("psID", playerScreenID);
		playerScreen.setMaxAge(24*60*60);
		response.addCookie(playerScreen);
	}
	
%>
<!doctype html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta http-equiv="cache-control" content="no-store, no-cache, must-revalidate, max-age=1" />
		<meta http-equiv="pragma" content="no-cache" />
		<meta http-equiv="expires" value="1" />
		<title>Two-screen | Enjoy a video</title>
		<link rel="shortcut icon" href="/images/favicon.ico" />
		<link href="/css/tv.css" rel="stylesheet" type="text/css" />
		<link rel="stylesheet" type="text/css" href="/css/jquery.fancybox.css?v=2.1.2" media="screen" />
		<script type="text/javascript" src='/_ah/channel/jsapi'></script>
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
		<script type="text/javascript" src="/js/jquery.fancybox.js?v=2.1.3"></script>
		<script type="text/javascript" src="/js/jquery.qrcode.js"></script>
		<script type="text/javascript" src="/js/qrcode.js"></script>
		<script type="text/javascript" src="/js/comm.js"></script>
		<script type="text/javascript" src="/js/cookie.js"></script>
		<script type="text/javascript" src="/js/youtube.js"></script>
		<script type="text/javascript">
			var times=<%= Event.getTimesJson(videoId) %>;
			var videoId = "<%= videoId %>";
			var playerScreenID = "<%= playerScreenID %>";
			function load() {
				requestToken();
				qr();
			}
			function qr() {
				var link = "http://" + window.location.host + "/app/index.jsp?psID=" + playerScreenID;
				jQuery('#qrcode').qrcode({width: 300,height: 300,text: link});
				jQuery('#qrcode-thumb').qrcode({width: 60,height: 60,text: link});
			}
			function showQR() {
				document.getElementById('light').style.display='block';
				document.getElementById('fade').style.display='block';
				document.getElementById('QRclose').focus();
			}
			function hideQR() {
				document.getElementById('light').style.display='none';
				document.getElementById('fade').style.display='none';
			}
		</script>
		<script type="text/javascript">
			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', 'UA-35281811-1']);
			_gaq.push(['_trackPageview']);
			
			(function() {
			  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
			})();
		</script>
	</head>
	<body onload="load();" onunload="">
	
		<div id="video">
			<div id="videoDiv">
				Loading...
			</div>
		</div>
		
		<div id="light" class="white_content">
			<a id="QRclose" href="javascript:void(0)" onclick="hideQR();">Close</a>
			<div id="qrcode"></div>
		</div>
		<div id="fade" class="black_overlay"></div>
		
	</body>
</html>